package com.zhycheng.readerxls;

import java.io.InputStream;
import java.lang.reflect.Member;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

	ArrayList<ArrayList<Provinces.Cities>> cityMaps;
	ArrayList<Provinces.Cities> cityMap;
	ArrayList<ArrayList<Provinces.Regions>> regionsMaps;
	ArrayList<Provinces.Regions> regionsMap;
	TextView tv;

	private ArrayList<Provinces> provinces = new ArrayList<Provinces>();
	private Provinces province;
	private ArrayList<Provinces.Cities> cities = new ArrayList<Provinces.Cities>();
	private Provinces.Cities city;
	private ArrayList<Provinces.Regions> Regions = new ArrayList<Provinces.Regions>();
	private Provinces.Regions Region;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.tv);
		cityMaps = new ArrayList<ArrayList<Provinces.Cities>>();
		regionsMaps = new ArrayList<ArrayList<Provinces.Regions>>();

        AssetManager am = this.getAssets();
        InputStream is = null;
        try {
			is = am.open("province.xls");
			Workbook wb = Workbook.getWorkbook(is);
			Sheet sheet0 = wb.getSheet(0);
			Sheet sheet1 = wb.getSheet(1);
			Sheet sheet2 = wb.getSheet(2);
			int row2 = sheet2.getRows();
			int row1 = sheet1.getRows();
			int row0 = sheet0.getRows();

			for(int i=1; i<row0; i++) {
				Cell ProvinceID = sheet0.getCell(0, i);
				Cell ProvinceName = sheet0.getCell(1, i);
				province = new Provinces(ProvinceName.getContents(),
						Integer.parseInt(ProvinceID.getContents()));
				provinces.add(province);

				cityMap = new ArrayList<Provinces.Cities>();
				for(int j=1; j<row1; j++) {
					Cell CityID = sheet1.getCell(0, j);
					Cell CityName = sheet1.getCell(1, j);
					Cell ProvinceID1 = sheet1.getCell(2, j);

					if (ProvinceID.getContents().equals(ProvinceID1.getContents())) {
						city = new Provinces.Cities(CityName.getContents(),
								Integer.parseInt(CityID.getContents()));
						cityMap.add(city);
					}

//					regionsMap = new ArrayList<Provinces.Regions>();
					for(int k=1; k<row2; k++) {
						Cell RegionID = sheet2.getCell(0, k);
						Cell RegionName = sheet2.getCell(1, k);
						Cell CityID1 = sheet2.getCell(2, k);

//						if (CityID.getContents().equals(CityID1.getContents())) {
//							Region = new Provinces.Regions(RegionName.getContents(),
//									Integer.parseInt(RegionID.getContents()));
//							regionsMap.add(Region);
//						}
					}
//					regionsMaps.add(regionsMap);
				}
				cityMaps.add(cityMap);

			}
			 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Gson gson = new Gson();
		JSONArray provincesArray = new JSONArray();
		for (int i = 0; i < provinces.size(); i++) {
			String provincesStr = gson.toJson(provinces.get(i));
			JSONObject provincesObject;

			cityMap = cityMaps.get(i);
			cities.clear();
			for (int a=0; a<cityMap.size(); a++){
				city = cityMap.get(a);
				cities.add(city);
			}

			try {
				JSONArray cityArray = new JSONArray();
				for (int j = 0; j < cities.size(); j++) {
					String cityStr = gson.toJson(cities.get(j));
					JSONObject cityObject;
					try {
						cityObject = new JSONObject(cityStr);
						cityArray.put(j, cityObject);
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}

				provincesObject = new JSONObject(provincesStr);
				provincesObject.put("city", cityArray);
				provincesArray.put(i, provincesObject);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		tv.setText(provincesArray.toString());
		Log.i("MainActivity", provincesArray.toString());

    }
}